﻿package com.taihua_tech.basic.modul1.controller;

import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.taihua_tech.basic.modul1.dao.RenHongDao;
import com.taihua_tech.basic.util.DataParam;
import com.taihua_tech.basic.util.DataRow;
 

@Controller
@RequestMapping("/modul1")
public class RenHongController {
	@Resource//注入
	private RenHongDao renHongDao;
	
	@RequestMapping("/renhong.html")
	public String initPage(ModelMap model,HttpServletRequest req,@RequestParam Map<String,String> map){
		System.out.println("进入了 HaoBoController 控制器方法");
		String msg=map.get("message");
		List<DataRow>list=renHongDao.findList(new DataParam());
		System.out.println(list);
		System.out.println(1112);
		model.addAttribute("renlist", list);
		model.addAttribute("message", msg);
		return "/modul1/RenhongPage";
	}
	
	@RequestMapping("/renhong.do")
	@ResponseBody
	public List<DataRow> findList(@RequestParam Map<String,String> map,HttpServletRequest req){
		System.out.println("map:"+map);
		System.out.println("param1:"+req.getParameter("param1"));
		DataParam param=new DataParam();
		System.out.println("任洪");
		List<DataRow>list=renHongDao.findList(param);
		return list;
	}
	
	
	//@RequestMapping(value = "/renhong2.do",method = RequestMethod.POST)
	@PostMapping ("/renhong2.do")
	@ResponseBody
	public List<DataRow> findList2(@RequestParam Map<String,String> map,HttpServletRequest req){
		System.out.println("map222:"+map);
		List<DataRow>list=renHongDao.findList(new DataParam());
		return list;
	}
	
	@PostMapping ("/renhong3.do")
	@ResponseBody
	public List<DataRow> findList2(@RequestBody DataParam param,HttpServletRequest req){
		System.out.println("param333:"+param);
		List<DataRow>list=renHongDao.findList(new DataParam());
		return list;
	}
	
	@PostMapping(value = "/{id}/hong.do", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	public String addPhoto(@PathVariable int id, HttpServletRequest request,RedirectAttributes redirectAttributes,
				@RequestParam("file_param") MultipartFile file) throws Exception {
		//////////////////////////////////////////////////////////////////////////////////
		System.out.println("id:===="+id);
		System.out.println("MediaType.MULTIPART_FORM_DATA_VALUE:"+MediaType.MULTIPART_FORM_DATA_VALUE);
//		//////////////////////////////////////////////////////////////////////////////////
		FileOutputStream fos = new FileOutputStream(request.getServletContext().getRealPath("/") + file.getOriginalFilename());
		IOUtils.copy(file.getInputStream(), fos);
		fos.close();
		//可用将重定向的参数传递出去
		redirectAttributes.addAttribute("message", "文件已上传:"+file.getOriginalFilename());
		return  "redirect:/modul1/renhong.html";
	}
}




